#!/usr/local/bin/python
# This script checks if the length of top hit is within 80-110% of query length.
# If top hit length is within that range, no further checking is required => passed.
# Otherwise, it reports the length of...  

import sys

filename = sys.argv[1]

f = open(filename, "r")

lines = f.readlines()

#Formatting for 4 significant figures (for decimals)
def fmt(f):
  st = '{0:.4}'.format(f)
  return st

#List length
num = len(lines)

i = 0

lower = 85 
upper = 115

while i < num:
  curr_line = lines[i].split('\t')
  curr_locustag = curr_line[1]
  curr_start = int(curr_line[2])
  curr_stop = int(curr_line[3])
  curr_frame = curr_line[4]

  #Check if the line examined is not the first line and last line
  #If not first and last of array list, has both previous and next lines
  if i != 0 and i != num-1:
    if curr_line[7] != '' and curr_line[6] != '':
      prev_line = lines[i-1].split('\t')
      prev_start = int(prev_line[2])
      prev_stop = int(prev_line[3])
      next_line = lines[i+1].split('\t')
      next_start = int(next_line[2])
      next_stop = int(next_line[3])
      if curr_frame == '+':
        gapsize = curr_start - prev_stop - 1
      elif curr_frame == '-':
        gapsize = next_start - curr_stop - 1
      else:
        print "Strand info missing!"
      curr_top1 = int(curr_line[7])
      curr_top10 = float(curr_line[8])
      curr_aalen = int(curr_line[6])
      percent_length = (float(curr_aalen)/curr_top1)*100
      percent_length_top10 = (float(curr_aalen)/curr_top10)*100
      if lower <= percent_length <= upper or lower <= percent_length_top10 <= upper:
        pass
      else:
        print curr_locustag, "\t", gapsize, "\t", curr_aalen, "\t", curr_top1, "\t", curr_top10, "\t", fmt(percent_length), "\t", fmt(percent_length_top10)

  #If first line, has no previous line but has next line
  elif i == 0:
    next_line = lines[i+1].split('\t')
    next_start = int(next_line[2])
    next_stop = int(next_line[3])
    if curr_frame == '+':
      gapsize = curr_start - 1
    elif curr_frame == '-':
      gapsize = next_start - curr_stop - 1
    else:
      print "Strand info missing!"
    curr_top1 = int(curr_line[7])
    curr_top10 = float(curr_line[8])
    curr_aalen = int(curr_line[6])
    percent_length = (float(curr_aalen)/curr_top1)*100
    percent_length_top10 = (float(curr_aalen)/curr_top10)*100
    if lower <= percent_length <= upper or lower <= percent_length_top10 <= upper:
      pass
    else:
      print curr_locustag, "\t", gapsize, "\t", curr_aalen, "\t", curr_top1, "\t", curr_top10, "\t", fmt(percent_length), "\t", fmt(percent_length_top10)

  #If last line, has no next line but has previous line
  elif i == num-1:
    prev_line = lines[i-1].split('\t')
    prev_start = int(prev_line[2])
    prev_stop = int(prev_line[3])
    if curr_frame == '+':
      gapsize = curr_start - prev_stop - 1
    elif curr_frame == '-':
      pass
    else:
      print "Strand info missing!"
    if curr_line[7] != '' and curr_line[6] != '':
      curr_top1 = int(curr_line[7])
      curr_top10 = float(curr_line[8])
      curr_aalen = int(curr_line[6])
      percent_length = (float(curr_aalen)/curr_top1)*100
      percent_length_top10 = (float(curr_aalen)/curr_top10)*100
      if lower <= percent_length <= upper or lower <= percent_length_top10 <= upper:
        pass
      else:
        print curr_locustag, "\t", "Last Element in List with negative frame!", "\t", curr_aalen, "\t", curr_top1, "\t", curr_top10, "\t", fmt(percent_length), "\t", fmt(percent_length_top10)

  i += 1

f.close()
